import {defHttp} from '/@/utils/http/axios';
import {Modal} from 'ant-design-vue';
import { useGlobSetting } from '/@/hooks/setting';
const globSetting = useGlobSetting();

export enum Api {
  list = "/system-service/config/page",
  getConfigById = "/system-service/config/getConfigById",
  getConfigByKey = "/system-service/config/getConfigByKey",
  // 获取敏感配置
  getSensitiveConfig = "/system-service/config/getSensitiveConfig",
  getOpenConfigByKey = "/system-service/config/open/getConfigByKey",
  getAppInitConfig = "/system-service/config/open/initConfig",
  save = "/system-service/config/save",
  edit = "/system-service/config/update",
  updateConfigParam = "/system-service/config/updateConfigParam",
  delete = "/system-service/config/delete",
  deleteBatch = "/system-service/config/deleteBatch",
  refreshCache = "/system-service/config/refreshCache",
  exportXls = "/system-service/config/exportXls",
  importExcel = "/system-service/config/importExcel",

  saveGroup = "/system-service/config/group/save",
  editGroup = "/system-service/config/group/update",
  deleteGroup = "/system-service/config/group/delete",
  deleteBatchGroup = "/system-service/config/group/batchDelete",
}

/**
 * 导出url
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入url
 */
export const getImportUrl = Api.importExcel;

export const listConfig = (params) => {
  return defHttp.get({url: Api.list, params});
}

export const getConfig = (params) => {
  return defHttp.get({url: Api.getConfigById, params});
}

export const getOpenConfig = (configKey) => {
  return defHttp.get({url: Api.getOpenConfigByKey, params: { configKey}});
}

export const getAppInitConfig = () => {
  return defHttp.get(
    {
      url: Api.getAppInitConfig, 
      params: { clientId: globSetting.oauth2ClientId }, 
      headers: {
        'x-tenant-id': globSetting.platformTenantId
      }
    }
  );
}

export const getConfigByKey = (configKey : string) => {
  return defHttp.get({url: Api.getConfigByKey, params: { configKey }});
}

export const getSensitiveConfig = (params) => {
  return defHttp.get({url: Api.getSensitiveConfig, params});
}


export const refreshCache = () => {
  return defHttp.delete({url: Api.refreshCache});
}

export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({url: url, params});
}

export const updateConfigParam = (params) => {
  return defHttp.post({url: Api.updateConfigParam, params});
}

export const getConfigParam = (configKey) => {
  return defHttp.get({url: Api.getConfigParam, params: {configKey}});
}


export const deleteConfig = (params, handleSuccess) => {
  return defHttp.delete({url: Api.delete, data: params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

export const batchDeleteConfig = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}


export const saveOrUpdateGroup = (params, isUpdate) => {
  let url = isUpdate ? Api.editGroup : Api.saveGroup;
  return defHttp.post({url: url, params});
}


export const deleteGroup = (params, handleSuccess) => {
  return defHttp.delete({url: Api.deleteGroup, data: params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}


export const batchDeleteGroup = (params, handleSuccess) => {
  console.log('params', params);
  
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatchGroup, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}